package com.lagou.ex.mybatis.base.controller;

import com.lagou.ex.mybatis.base.model.UserModel;
import com.lagou.ex.mybatis.base.model.req.CreateUserReq;
import com.lagou.ex.mybatis.base.model.req.UserReq;
import com.lagou.ex.mybatis.base.model.resp.UserResp;
import com.lagou.ex.mybatis.base.service.IUserService;
import com.lagou.ex.mybatis.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
public class UserController {
    @Autowired
    private IUserService userService;

    /**
     * 通过手机查询用户信息
     *
     * @param phone 手机号码
     * @return
     */
    @RequestMapping("/api/user/queryByPhone")
    public UserResp queryByPhone(String phone) {
        UserReq userReq = new UserReq();
        userReq.setPhone(phone);
        log.info("/api/user/queryByPhone in params-{}", userReq);
        List<UserModel> userModels = userService.queryByPhone(userReq.getPhone());
        UserResp userResp = new UserResp();
        userResp.setUserModelList(userModels);
        return userResp;
    }

    @PostMapping("/api/user/createUser")
    public UserResp createUser(@RequestBody CreateUserReq userReq) {
        log.info("/api/user/queryByPhone in params-{}", userReq);
        boolean flag = userService.createUser(userReq.getUserModel());
        return ResultUtil.build(UserResp.class, flag);
    }

}
